﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Diagnostics;
using BusinessTier;

public partial class Game : System.Web.UI.Page
{

    // <TEMPORARY>
    internal static MazeServer game = MazeServerFactory.getMazeServer(MazeServerFactory.createMazeServer());
    public Player thisPlayer;
    // </TEMPORARY>


    public string mazeDataJson = "";
    private DateTime start;

    protected void Page_Load(object sender, EventArgs e)
    {
        // redirects if not logged in
        Object username = Session["username"];
        if ((username == null) || (username.ToString().CompareTo("") == 0))
        {
            Response.Redirect("Login.aspx?Redirect=Game");
        }
        else
        {
            // <TEMPORARY>
            game.addPlayer(new PlayerService().createPlayer(username.ToString(), game));
            foreach (String user in game.Users)
            {
                ListItem li = new ListItem();
                li.Text = user.ToString();
                PlayerList.Items.Add(li);
            }
            // </TEMPORARY>

            MazeServer server = new MazeServer("adfaeff");
            mazeDataJson = server.GetMazeJsonString(
            int.Parse(size_box.Text), int.Parse(size_box.Text));

            if (!this.IsPostBack)
            {
                start = DateTime.Now;
            }
        }
    }

    protected void UpdateTimer_Tick(object sender, EventArgs e)
    {
        int diff_ms_all = DateTime.Now.Millisecond - start.Millisecond;
        int diff_ms = diff_ms_all % 1000;
        int diff_s = DateTime.Now.Second - start.Second;
        int diff_m = DateTime.Now.Minute - start.Minute;
    }

    [WebMethod]
    public static string BestScore(string score)
    {
        String username = HttpContext.Current.Session["username"].ToString();
        var client = new BusinessTier.DataManipulation();

        String result = (string)client.NewBestRecord(score, username);

        if (String.Compare(result, "200") == 0)
        {
            result = (string)client.GetBestRecord(username);
        }
        return result;
    }

    [WebMethod]
    public static void Finish_Maze()
    {
        HttpContext.Current.Response.Redirect("Finish.aspx");
    }

    [WebMethod]
    public static string UpdateMovement(long timestamp, int x, int y, string direction)
    {
        GameEvent e = new GameEvent(timestamp, GameEventType.Move, HttpContext.Current.Session["username"].ToString());
        e.PutData("timestamp", timestamp);
        e.PutData("source", HttpContext.Current.Session["username"].ToString());
        e.PutData("x", x);
        e.PutData("y", y);
        e.PutData("direction", direction);
        game.OnEventHappened(e);
        return e.ToString();
    }

    [WebMethod]
    public static string Halt(long timestamp, int x, int y)
    {
        GameEvent e = new GameEvent(timestamp, GameEventType.Stop, HttpContext.Current.Session["username"].ToString());
        e.PutData("timestamp", timestamp);
        e.PutData("source", HttpContext.Current.Session["username"].ToString());
        e.PutData("x", x);
        e.PutData("y", y);
        game.OnEventHappened(e);
        return e.ToString();
    }

    [WebMethod]
    public static IList<IDictionary<string, object>> CheckChanges()
    {
        Player me = new PlayerService().getPlayer(HttpContext.Current.Session["username"].ToString());
        long start = DateTime.Now.Ticks;
        return null;
    }
}
